package com.ruoyi.stock.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.stock.domain.StockInventory;
import com.ruoyi.stock.service.IStockInventoryService;
import com.ruoyi.api.service.IStockService;
import com.ruoyi.api.dto.StockInventoryDTO;

/**
 * 库存服务实现类
 * 
 * @author ruoyi
 * @date 2025-06-21
 */
@Service("stockService")
public class StockServiceImpl implements IStockService 
{
    @Autowired
    private IStockInventoryService stockInventoryService;

    /**
     * 检查商品库存是否足够
     * 
     * @param productId 商品ID
     * @param quantity 需要的数量
     * @return true:库存足够, false:库存不足
     */
    @Override
    public boolean checkStockSufficient(Long productId, Integer quantity) 
    {
        if (productId == null || quantity == null || quantity <= 0) {
            return false;
        }
        
        StockInventory query = new StockInventory();
        query.setProductId(productId);
        List<StockInventory> list = stockInventoryService.selectStockInventoryList(query);
        
        if (list == null || list.isEmpty()) {
            return false;
        }
        
        StockInventory inventory = list.get(0);
        return inventory.getCurrentQuantity() >= quantity;
    }
    
    /**
     * 通过商品ID查询库存信息
     * 
     * @param productId 商品ID
     * @return 库存信息
     */
    @Override
    public StockInventoryDTO getInventoryByProductId(Long productId) 
    {
        if (productId == null) {
            return null;
        }
        
        StockInventory query = new StockInventory();
        query.setProductId(productId);
        List<StockInventory> list = stockInventoryService.selectStockInventoryList(query);
        
        if (list == null || list.isEmpty()) {
            return null;
        }
        
        return convertToDTO(list.get(0));
    }
    
    /**
     * 查询库存列表
     * 
     * @param inventoryDTO 库存信息
     * @return 库存列表
     */
    @Override
    public List<StockInventoryDTO> selectInventoryList(StockInventoryDTO inventoryDTO) 
    {
        StockInventory query = new StockInventory();
        if (inventoryDTO != null) {
            query.setProductId(inventoryDTO.getProductId());
            query.setProductName(inventoryDTO.getProductName());
            query.setWarehouseCode(inventoryDTO.getWarehouseCode());
            query.setStatus(inventoryDTO.getStatus());
        }
        
        List<StockInventory> list = stockInventoryService.selectStockInventoryList(query);
        return convertToDTOList(list);
    }
    
    /**
     * 将实体对象转换为DTO
     * 
     * @param inventory 库存实体
     * @return 库存DTO
     */
    private StockInventoryDTO convertToDTO(StockInventory inventory) 
    {
        if (inventory == null) {
            return null;
        }
        
        StockInventoryDTO dto = new StockInventoryDTO();
        dto.setInventoryId(inventory.getInventoryId());
        dto.setProductId(inventory.getProductId());
        dto.setProductName(inventory.getProductName());
        dto.setCurrentQuantity(inventory.getCurrentQuantity());
        dto.setUpperLimit(inventory.getUpperLimit());
        dto.setLowerLimit(inventory.getLowerLimit());
        dto.setWarehouseCode(inventory.getWarehouseCode());
        dto.setLocationCode(inventory.getLocationCode());
        dto.setStatus(inventory.getStatus());
        
        return dto;
    }
    
    /**
     * 将实体列表转换为DTO列表
     * 
     * @param list 库存实体列表
     * @return 库存DTO列表
     */
    private List<StockInventoryDTO> convertToDTOList(List<StockInventory> list) 
    {
        List<StockInventoryDTO> dtoList = new ArrayList<>();
        
        if (list != null && !list.isEmpty()) {
            for (StockInventory inventory : list) {
                StockInventoryDTO dto = convertToDTO(inventory);
                if (dto != null) {
                    dtoList.add(dto);
                }
            }
        }
        
        return dtoList;
    }
} 